function getCoursesCountByTab() {
    var coursesTab = ["Python", "C/C++", "信息安全", "Linux", "Java", "PHP", "HTML5", "NodeJS", "Docker"];
    var obj = new Object();
    obj.coursesTab = coursesTab + "";
    var token = getCookie("token");
    $.ajax({
        url: "/apis/course/get_courses_count_by_tab?coursesTab="+coursesTab,
        type: "GET",
        headers: {
            token: token //这里是Token
        },
        cache: false,//设置不缓存
        success: getCoursesCountByTabSuccess,
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            showErrorInfo(XMLHttpRequest,textStatus,errorThrown);
        }
    });
}

function getCoursesCountByTabSuccess(data) {
    var obj = JSON.parse(data);
    var coursesCountList = obj['coursesCountList'];
    document.getElementById("pythonCoursesCount").innerHTML = coursesCountList[0];
    document.getElementById("cppCoursesCount").innerHTML = coursesCountList[1];
    document.getElementById("informationSafetyCoursesCount").innerHTML = coursesCountList[2];
    document.getElementById("linuxCoursesCount").innerHTML = coursesCountList[3];
    document.getElementById("javaCoursesCount").innerHTML = coursesCountList[4];
    document.getElementById("phpCoursesCount").innerHTML = coursesCountList[5];
    document.getElementById("html5CoursesCount").innerHTML = coursesCountList[6];
    document.getElementById("nodejsCoursesCount").innerHTML = coursesCountList[7];
    document.getElementById("dockerCoursesCount").innerHTML = coursesCountList[8];
}

function getCoursesTop4() {
    var token = getCookie("token");
    $.ajax({
        url: "/apis/course/get_courses_top_number?topNumber=4",
        type: "GET",
        cache: false,//设置不缓存
        headers: {
            token: token //这里是Token
        },
        success: getCoursesTop4Success,
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            showErrorInfo(XMLHttpRequest,textStatus,errorThrown);
        }
    });
}

function getCoursesTop4Success(data) {
    var coursesJson = JSON.parse(data);
    if(!(coursesJson.result ==="success")) {
        document.getElementById("coursesList").innerHTML = "暂无课程！";
    } else {
        var courseList = coursesJson['courses'];
        var str = "";
        for (var i = 0; i < courseList.length; i++) {
            str += "<div class=\"col-md-3 col-sm-6  course\">";
            str += "<a class=\"course-box\" href=\"courses/show.html?courseId=" + courseList[i].id + "\">";
            str += "<div class=\"sign-box\">";
            str += "<i class=\"fa fa-star-o course-follow pull-right\"";
            str += "data-follow-url=\"/courses/1/follow\"";
            str += "data-unfollow-url=\"/courses/1/unfollow\"  style=\"display:none\"  ></i>";
            str += "</div>";
            str += "<div class=\"course-img\">";
            str += "<img alt=\"" + courseList[i].name + "\" src=\"" + courseList[i].imgWebUrl + "\">";
            str += "</div>";
            str += "<div class=\"course-body\">";
            str += "<span class=\"course-title\" data-toggle=\"tooltip\" data-placement=\"bottom\" title=\"" + courseList[i].name + "\">" + courseList[i].name + "</span>";
            str += "</div>";
            str += "<div class=\"course-footer\">";
            str += "<span class=\"course-per-num pull-left\">";
            // str += "<i class=\"fa fa-users\"></i>";
            str += "最后修改于：" + courseList[i].modificationTime.split(".")[0];
            str += "</span>";
            str += "</div>";
            str += "</a>";
            str += "</div>";
        }
        document.getElementById("coursesList").innerHTML = str;
        if(courseList.length >= 4) {
            var moreCourses = "<div class=\"pull-right watch-all\">";
            moreCourses += "<a href=\"courses/index.html\">查看更多 ></a>";
            moreCourses += "</div>";
            document.getElementById("moreCourses").innerHTML = moreCourses;
        }
    }
}

$(document).ready(function () {
        getCoursesTop4();
        getCoursesCountByTab();
});